package com.tanhua.manage.controller;

import com.tanhua.domain.db.AccountThaw;
import com.tanhua.domain.db.UserFreeze;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.manage.service.UserService;


import org.apache.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Handler;

@RestController
@RequestMapping("manage")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 接口名称：用户数据翻页
     * 接口路径：GET/manage/users
     */
    @GetMapping("users")
    public ResponseEntity<Object> findByPage(Integer page, Integer pagesize) {
        return userService.findByPage(page, pagesize);
    }

    /**
     * 接口名称：用户基本资料
     * 接口路径：GET/manage/users/:userID
     */
    @GetMapping("/users/{userId}")
    public ResponseEntity<Object> findById(@PathVariable("userId") Long userId) {
        return userService.findById(userId);
    }

    /**
     * 接口名称：视频记录翻页
     * 接口路径：GET/manage/videos
     */
    @GetMapping("/videos")
    public ResponseEntity<Object> findVideoList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize, Long uid) {
        return userService.findVideoList(page, pagesize, uid);
    }

    /**
     * 接口名称：动态分页
     * 接口路径：GET/manage/messages
     */
    //========================================
    @GetMapping("messages")
    public ResponseEntity<Object> findMovementsList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize, Long uid,
            @RequestParam(required = false, value = "state") String strState) {
        Long state = null;
        strState = StringUtils.equals("", strState) || StringUtils.equals("''", strState) ? null : strState;
        if (strState != null) {
            state = Long.valueOf(strState);
        }
        return userService.findMovementsList(page, pagesize, uid, state);
    }

    /**
     * 接口名称：动态详情
     * 接口路径：GET/manage/messages/:id
     */
    @GetMapping("/messages/{id}")
    public ResponseEntity<Object> findMovementsById(@PathVariable("id") String publishId) {
        return userService.findMovementsById(publishId);
    }

    /**
     * 接口名称：评论列表翻页
     * 接口路径：GET/manage/messages/comments
     */
    @GetMapping("/messages/comments")
    public ResponseEntity<Object> findCommentsById(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,
            @RequestParam(name = "messageID") String publishId) {
        return userService.findCommentsById(publishId, page, pagesize);
    }

    /**
     * 接口名称：用户冻结操作
     * 接口路径：POST/manage/users/freeze
     */
    @PostMapping("users/freeze")
    public ResponseEntity<Object> freezeAccount(@RequestBody UserFreeze userFreeze) {
        Integer userId = userFreeze.getUserId();
        UserInfo userInfo = userInfoApi.findById(userId.longValue());
        userInfo.setUserStatus("2");
        //把冻结状态写入userInfo表
        userInfo.setReasonsForThawing(" ");
        userInfoApi.update(userInfo);
        //冻结时间存入redis中
        Integer freezingTime = userFreeze.getFreezingTime();
        switch (freezingTime) {
            case 1:

                redisTemplate.opsForValue().set("user_freezing_time"+userId,"3",3, TimeUnit.DAYS);

                break;
            case 2:
                redisTemplate.opsForValue().set("user_freezing_time"+userId,"7",7, TimeUnit.DAYS);
                break;
            case 3:
                redisTemplate.opsForValue().set("user_freezing_time"+userId,"永久");
                break;
        }


        //调用方法 添加到冻结状态表
        Integer integer = userService.freezeAccount(userFreeze);
        Map<String,String> result = new HashMap<>();

        if (integer==1) {
            result.put("message","操作成功");

        }else {
            result.put("message","操作失败");
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 接口名称：用户解冻操作
     * 接口路径：POST/manage/users/unfreeze
     */
    @PostMapping("users/unfreeze")
    public ResponseEntity<Object> accountThaw(@RequestBody AccountThaw accountThaw) {
        Integer userId = accountThaw.getUserId();
        String reasonsForThawing = accountThaw.getReasonsForThawing();
        UserInfo userInfo = userInfoApi.findById(userId.longValue());
        userInfo.setUserStatus("1");
        userInfo.setReasonsForThawing(reasonsForThawing);
        //解冻删除redis中的记录
        if (redisTemplate.opsForValue().get("user_freezing_time_1"+userId) != null) {
            redisTemplate.delete("user_freezing_time_1"+userId);

        }else if (redisTemplate.opsForValue().get("user_freezing_time_2"+userId) != null) {
            redisTemplate.delete("user_freezing_time_2"+userId);

        }else {
            redisTemplate.delete("user_freezing_time_3" + userId);
        }
        //把解冻后的状态更新到userInfo表中
        Integer integer = userService.accountThaw(userInfo);

        userService.delectUnfreeze(userId);
        Map<String,String> result = new HashMap<>();

        if (integer==1) {
            result.put("message","操作成功");

        }else {
            result.put("message","操作失败");
        }
        return ResponseEntity.ok(result);
    }


    //=================================================
    /**
     * 接口名称：动态通过
     * 接口路径：POST/manage/messages/pass
     */
    @PostMapping("/messages/pass")
    public ResponseEntity<Object> statePass(@RequestBody String[] pubilshList) {
        log.info("_______接口名称：动态通过_______");
        return userService.statePass(pubilshList);
    }

    //=================================================
    /**
     * 接口名称：动态拒绝
     * 接口路径：POST/manage/messages/reject
     */
    @PostMapping("/messages/reject")
    public ResponseEntity<Object> stateReject(@RequestBody String[] pubilshList) {
        log.info("_______接口名称：动态拒绝_______");
        return userService.stateReject(pubilshList);
    }
}
